package com.sqx.modules.message.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.annotation.LoginUser;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fang
 * @date 2020/7/13
 */
@RestController
@Api(value = "消息管理", tags = {"消息管理"})
@RequestMapping(value = "/app/message")
public class AppMessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/page/{state}/{page}/{limit}", method = RequestMethod.GET)
    @ApiOperation("管理平台分页查询公告接口")
    @ResponseBody
    public Result getBodyPage(@PathVariable Integer state, @PathVariable Integer page, @PathVariable int limit,Integer type) {
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("state",state);
        map.put("type",type);
        map.put("userId",null);
        return Result.success().put("data",messageService.selectMessageList(map));
    }

    @Login
    @RequestMapping(value = "/selectMessageByUserId", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectUserRecharge(int page, int limit,@RequestAttribute("userId") Long userId,Integer state,Integer platform,Integer status){
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("userId",userId);
        map.put("state",state);
        map.put("platform",platform);
        map.put("status",status);
        PageUtils pageUtils = messageService.selectMessageList(map);
        messageService.updateSendState(userId,state);
        return Result.success().put("data",pageUtils);
    }

    @Login
    @RequestMapping(value = "/selectMessageByUserIdLimit1", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectMessageByUserIdLimit1(@RequestAttribute("userId") Long userId){
        Map<String,Object> map=new HashMap<>();
        map.put("page",1);
        map.put("limit",1);
        map.put("userId",userId);
        return Result.success().put("data",messageService.selectMessageList(map));
    }

    @Login
    @GetMapping("/selectMessageById")
    @ApiOperation("根据id查详情")
    public Result selectMessageById(Long id){
        return Result.success().put("data",messageService.getById(id));
    }


    @Login
    @PostMapping("/insertMessage")
    @ApiOperation("添加投诉")
    public Result insertMessage(@LoginUser UserEntity user, @RequestBody MessageInfo messageInfo){
        if ("8".equals(messageInfo.getState())){
            UserEntity userEntity = userService.getById(user.getUserId());
            messageInfo.setTitle(userEntity.getPhone());
            messageInfo.setContent("注销");
            int count = messageService.count(new QueryWrapper<MessageInfo>().eq("user_id", user.getUserId()).eq("state", 8));
            if (count>0){
                return Result.error("注销申请已提交,请勿重复提交");
            }
        }


        messageInfo.setUserName(user.getUserName());
        messageInfo.setUserId(String.valueOf(user.getUserId()));
        messageInfo.setIsSee("2");
        messageService.saveBody(messageInfo);
        return Result.success();
    }

    @Login
    @PostMapping("/updateMessage")
    @ApiOperation("修改")
    public Result updateMessage(@RequestBody MessageInfo messageInfo){
        messageService.update(messageInfo);
        return Result.success();
    }

    @Login
    @PostMapping("/deleteMessage")
    @ApiOperation("删除")
    public Result deleteMessage(Long id){
        messageService.delete(id);
        return Result.success();
    }

    @GetMapping("/selectMessageCount")
    @ApiOperation("查询消息总数")
    @Login
    public Result selectMessageCount(@RequestAttribute("userId")Long userId){
        return Result.success().put("data",messageService.count(new QueryWrapper<MessageInfo>().eq("is_see",0).eq("user_id", userId)));
    }


}